package com.bw.exam.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.exam.pojo.Brand;
import com.bw.exam.pojo.Specification;
import com.bw.exam.pojo.SpecificationOption;
import com.bw.exam.result.R;
import com.bw.exam.service.SpecificationOptionService;
import com.bw.exam.service.SpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("manage/specification")
@Slf4j
public class SpecificationController {


    @Resource
    private SpecificationService specificationService;

    @Autowired
    private SpecificationOptionService specificationOptionService;


    // restful风格传参
    @RequestMapping("list/{startPage}/{endPage}")
    public R list(@RequestParam("specName")String specName, @PathVariable int startPage, @PathVariable int endPage){

        QueryWrapper queryWrapper = new QueryWrapper();

        // 先判断名称不为空去走查询条件
        if(StringUtils.isNotBlank(specName)){
            queryWrapper.like("spec_name",specName);
        }
        // 进行分页处理
        Page<Specification> page = new Page<Specification>(startPage,endPage);
        Page<Specification> reponsePage = specificationService.page(page,queryWrapper);
        // 进行返回结果处理
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",reponsePage.getRecords());
        map.put("total",reponsePage.getTotal());

        return R.success(map);
    }

    // restful风格传参
    @RequestMapping("list/all")
    public R listall(){

        return R.success(this.specificationService.list());
    }


    //添加接口
    @RequestMapping("/add")
    public R add(@RequestBody Specification specification){
        log.info("specification 添加 specification:{}", JSON.toJSONString(specification));
        this.specificationService.add(specification);
        return R.success();
    }


    //添加接口
    @RequestMapping("/update")
    public R update(@RequestBody Specification specification){
        log.info("specification 修改 specification:{}", JSON.toJSONString(specification));
        this.specificationService.update(specification);
        return R.success();
    }


    //查询接口
    @RequestMapping("/findById/{id}")
    public R update(@PathVariable Long id){
        log.info("specification 查询 specification:{}",id);
        Specification specification = this.specificationService.getById(id);

        LambdaQueryWrapper<SpecificationOption> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SpecificationOption::getSpecId,id);
        List<SpecificationOption> list = this.specificationOptionService.list(lambdaQueryWrapper);

        specification.setSpecificationOptionList(list);

        return R.success(specification);
    }



    //删除接口
    @RequestMapping("/delById/{id}")
    public R del(@PathVariable Long id){
        log.info("specification 删除 specification:{}",id);
        this.specificationService.del(id);
        return R.success();
    }



}















